package com.aljoi.tools.demo.ui;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.aljoi.iframe.mvp.XActivity;
import com.aljoi.iframe.net.upload.FileUploadObserver;
import com.aljoi.iframe.net.upload.RetrofitClient;
import com.aljoi.tools.demo.R;
import com.daimajia.numberprogressbar.NumberProgressBar;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;

public class UploadActivity extends XActivity {


    String url = "http://192.168.1.61:8080/UploadFileServer/servlet/UploadHandleServlet";
    File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "11111.jpg");
    File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "22222.jpg");
    File file3 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "33333.jpg");
    File file4 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "44444.jpg");
    File file5 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "55555.jpg");
    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.number_progress_bar1)
    NumberProgressBar numberProgressBar1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.number_progress_bar2)
    NumberProgressBar numberProgressBar2;
    @BindView(R.id.btn3)
    Button btn3;
    @BindView(R.id.number_progress_bar3)
    NumberProgressBar numberProgressBar3;
    @BindView(R.id.btn4)
    Button btn4;
    @BindView(R.id.number_progress_bar4)
    NumberProgressBar numberProgressBar4;
    @BindView(R.id.btn5)
    Button btn5;
    @BindView(R.id.number_progress_bar5)
    NumberProgressBar numberProgressBar5;

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_upload;
    }

    @Override
    public Object newP() {
        return null;
    }

    /**
     * 上传方法
     */
    public void upload(final int n, File file) {
        RetrofitClient
                .getInstance()
                .upLoadFile(url, file, new FileUploadObserver<ResponseBody>() {
                    @Override
                    public void onUpLoadSuccess(ResponseBody responseBody) {
                        Toast.makeText(UploadActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                        try {
                            Log.d("上传进度", responseBody.string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onUpLoadFail(Throwable e) {
                        Toast.makeText(UploadActivity.this, "上传失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onProgress(int progress) {
                        Message message = new Message();
                        message.what = n;
                        message.arg1 = progress;
                        handler.sendMessage(message);

                    }
                });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    numberProgressBar1.setProgress(msg.arg1);
                    break;
                case 1:
                    numberProgressBar2.setProgress(msg.arg1);
                    break;
                case 2:
                    numberProgressBar3.setProgress(msg.arg1);
                    break;
                case 3:
                    numberProgressBar4.setProgress(msg.arg1);
                    break;
                case 4:
                    numberProgressBar5.setProgress(msg.arg1);
                    break;
            }
        }
    };


    @OnClick({R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                upload(0,file1);
                break;
            case R.id.btn2:
                upload(1,file2);
                break;
            case R.id.btn3:
                upload(2,file3);
                break;
            case R.id.btn4:
                upload(3,file4);
                break;
            case R.id.btn5:
                upload(4,file5);
                break;
        }
    }
}
